$(function(){
	//退出登录
	var logOff=$('#logOff');
	logOff.on('tap',function(e){
		localStorage.clear();
		window.location.href='./login.html';
		e.preventDefault();
	    return false;
	});
	var $showName=$('.person-Info');
	var $IDcardInfo=$('#IDcardInfo');
	var $gmaPersonInfo=$('#gma-personInfo');
	var $maskControl=$('#mask');
	var $alertControl=$('#alert');
	var $alertSure=$('#alert-sure');//只有确定按钮的弹窗
	var $alertContent=$alertControl.children('.alert-content');
	var data=common.getLocalData('H5-agentInfo');
	var globalHost=common.globalHost;
	var getVisitPointPort=common.getVisitPointByAgentId;
	var getVisitPointUrl=globalHost+getVisitPointPort;
	if(!data){
		window.location.href="login.html";
	}else{
		var token=common.getToken();
	}
	if(data.id && token){
		var url=common.globalHost+common.getAgentDetailPort;
		var sendData={id:data.id,token:token};
		requestDataIsAuth({
			sendData:sendData,
			url:url,
			callback:function(data){
				personInfo({
					dataList:data,
					targetSelector:$showName
				});
				identityState({
					dataList:data,
					targetSelector:$IDcardInfo
				});
				_pressPersonInfo(data['isAuth']);
			}
		})
		
	}else{
		window.location.href="login.html";
	}
	function _pressPersonInfo(isAuth){
		$gmaPersonInfo.off('touchend').on('touchend',function(e){
			if(isAuth==1){
	            $maskControl.removeClass('mask-hide').addClass("mask-show");
	            $alertSure.removeClass('mask-hide').addClass("mask-show");
	            $alertSure.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，请等待审核");
	        }else if(isAuth==3){
	        	window.location.href='./previewPersonInfo.html';
	        }else{
	            $maskControl.removeClass('mask-hide').addClass("mask-show");
	            $alertControl.removeClass('mask-hide').addClass("mask-show");
	            $alertControl.children('.alert-content').html("这是通过身份认证的经纪人才能享有的专属福利，快去认证吧！")
	        }
			isAuthAlert(isAuth);
	    })
	} 
	function isAuthAlert(isAuth){
		//点击取消按钮
		 $alertControl.children('.alert-cancel').off('tap').on('tap',function(e){
		    $maskControl.removeClass('mask-show').addClass("mask-hide");
		    $alertControl.removeClass('mask-show').addClass("mask-hide");
		    e.preventDefault();
		    return false;
		});
		//点击确定按钮
		$alertControl.children('.alert-sure').off('tap').on('tap',function(e){
		    $maskControl.removeClass('mask-show').addClass("mask-hide");
		    $alertControl.removeClass('mask-show').addClass("mask-hide");
		    if(isAuth==2){
		        window.location.href="registerFail.html";
		    }else if(isAuth==0){
		        window.location.href="registerInfoAgain.html";
		    }else if(isAuth==1){
		        window.location.href="registerAuth.html";
		    }
		    e.preventDefault();
		    return false;
		});
		//只有确定按钮的不进行操作
		$alertSure.children('.alert-sure').off('tap').on('tap',function(e){
		    $maskControl.removeClass('mask-show').addClass("mask-hide");
		    $alertSure.removeClass('mask-show').addClass("mask-hide");
		    e.preventDefault();
		    return false;
		});
	}
	function identityState(options){
		var targetSelector=$(options.targetSelector);
		var dataList=options.dataList;
		var isAuth=dataList.isAuth;
		var attrHref='';
		var childrenTagA=targetSelector.children('a');
		var childrenTagSpan=targetSelector.find('.gma-agentName');
		var spanInnerHTML="";
		if(isAuth===0){
			//未认证
			attrHref="registerInfoAgain.html";
			spanInnerHTML="请进行身份认证";
		}else if(isAuth===1){
			//认证中
			attrHref="registerAuth.html";
			spanInnerHTML="认证审核中";
		}else if(isAuth===2){
			//未通过
			attrHref="registerFail.html";
			spanInnerHTML="未通过，请重新认证";
		}else if(isAuth===3){
			//认证通过 停留在当前页面	
			attrHref="registerSuccess.html";
			spanInnerHTML="";
		}
		childrenTagA.attr('href',attrHref);
		childrenTagSpan.html(spanInnerHTML);
	};
	//头部个人信息，认证状态
	function personInfo(options){
		var targetSelector=$(options.targetSelector);
		var dataList=options.dataList;
		var status='';
		var isAuth=dataList.isAuth;
		var className='';
		if(isAuth==3){
			status='已认证';
			className="person-status-success"
		}else{
			status='未认证';
		}
		var agentName=dataList.agentName;
		agentName=!!agentName?agentName:"金牌经纪人";
		var point=!!dataList['point']?dataList['point']:0;
		var template='<p class="person-name">'+agentName+'<span class="'+className+'"></span></p>'+
				'<p class="person-tel">'+data.mobile+'</p>'+
				'<p><span>积分：'+point+'</span><i>1积分=1元钱</i></p>';
		targetSelector.html(template);
	};
	function requestDataIsAuth(options) {
        var url=options.url;
        var sendData=options.sendData;
        var callback=options.callback;
        $.ajax({
            data:sendData,
            type: "POST",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            dataType:"json",
            url:url,
            success:function(data){
                if(typeof data==="string"){
                    data=JSON.parse(data);
                };
                if(data.code==101 ||data.code==102){
                    console.log("token为空或者token失效");
                    window.location.href='login.html';
                };
                if(!!data && data['code']=="0"){
                        data=data.res.data;
                };   
                callback(data);         
            },
            timeout:10000,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                common.errDialog("网络不畅，请稍后重试");
                
            }
        });
    };
	
})